#

import asyncio

from crawl import Crawler, Tester
from settings import config
from apiserver import app


class Crond(object):
    async def cron_crawler(self):
        crawler = Crawler()
        while True:
            if not crawler.flush(config['proxy_key'], config['flush_score']):
                await asyncio.sleep(config['crawl_delay'])
                continue
            await crawler.run()
            await asyncio.sleep(config['crawl_delay'])

    async def cron_tester(self):
        tester = Tester()
        while True:
            await tester.run()
            await asyncio.sleep(config['test_delay'])

    def cron_api(self):
        app.run(config['api_host'], config['api_port'])

    async def run(self):
        await asyncio.gather(
            self.cron_crawler(),
            self.cron_tester()
        )
